<template>
	<view>
		<view v-if="PageCur=='home'">
			<home ref="cc"></home>
		</view>
		<call v-if="PageCur=='call'"></call>
		<my v-if="PageCur=='my'"></my>

		<!-- 自定义tabs -->
		<view class="cu-bar tabbar bg-white shadow foot">
			<view class="action" @click="NavChange" data-cur="home">
				<view class='cuIcon-cu-image'>
					<image :src="'/static/tabbar/icon_gerenzhongxin' + [PageCur=='home'?'_pre':'_nor'] + '.png'">
					</image>
				</view>
				<view :class="PageCur=='home'?'text-system-color':'text-gray'">首页</view>
			</view>
			<view v-if="isAdmin" class="action" @click="NavChange" data-cur="call">
				<view class='cuIcon-cu-image'>
					<image :src="'/static/tabbar/icon_yijianbaojing' + [PageCur == 'call'?'_pre':'_nor'] + '.png'">
					</image>
				</view>
				<view :class="PageCur=='call'?'text-system-color':'text-gray'">一键报警</view>
			</view>
			<view class="action" @click="NavChange" data-cur="my">
				<view class='cuIcon-cu-image'>
					<image :src="'/static/tabbar/icon_gerenzhongxin' + [PageCur == 'my'?'_pre':'_nor'] + '.png'">
					</image>
				</view>
				<view :class="PageCur=='my'?'text-system-color':'text-gray'">个人中心</view>
			</view>
		</view>

		<view class="cu-modal" :class="alertModal">
			<view class="cu-dialog">
				<view class="bg-img" :style="{backgroundImage: 'url('+alertImg+')'}"> </view>
				<view class="cu-bar bg-white" style="display: flex;flex-direction: column;">
					<view class="padding-xl" style="color: red;">{{alertTitle}}</view>
					<text
						style="width: 80%;border: 1px solid red;text-align: center;margin-bottom: 40rpx;">{{alertMsg}}</text>
					<view style="line-height: 30px;margin: 10rpx; 100rpx;height: 60px; width: 40%;"
						class="action bg-system-orange flex-sub  solid-left" @tap="sub">确认</view>
				</view>
			</view>
		</view>


	</view>
</template>

<script>
	import * as constants from '@/common/config.js';
	export default {
		onShow(option) {

			if (option !== undefined && option !== null && option !== "") {
				if (option.type === "refreshPage") {
					uni.showToast({
						title: "正在刷新...",
						icon: 'loading'
					})
					uni.$emit('refreshPage', {})
				}
			}
		},
		data() {
			return {
				PageCur: 'home',
				isAdmin: false,
				alertModal: '',
				alertTitle: 'xxx',
				alertMsg: 'xxxxx',
				alertImg: '../../static/index/banner2.png',
				infoType: '',
				noticeInfoId: '',
				equRounds: false,
				ucId: '',
				innerAudioContext: null,
				audioId: 0
			}
		},
		methods: {
			NavChange: function(e) {
				this.PageCur = e.currentTarget.dataset.cur
			},
			sub() {
				let that = this
				that.alertModal = ''
				if (that.equRounds) {
					// 预警详情页面
					uni.navigateTo({
						url: '../../yzqy/yjxq/yjxq?ucId=' + that.ucId + '&noticeInfoId=' + that.noticeInfoId
					})
				}
				// 直接跳转到报警成功页面
				else {
					uni.navigateTo({
						url: '../../yzqy/bjcg/bjcg?noticeInfoId=' + that.noticeInfoId + '&audioId=' + that.audioId
					})
				}
			},
			playAudio(path) {
				this.innerAudioContext = uni.createInnerAudioContext();
				this.innerAudioContext.src = path
				this.innerAudioContext.play()
			},
			stopAudio() {
				if (this.innerAudioContext) {
					this.innerAudioContext.stop()
				}
			}
		},
		destroyed() {
			this.stopAudio()
		},
		onLoad() {
			if (getApp().globalData.role == 'D') {
				this.isAdmin = true;
			} else {
				this.isAdmin = false;
			}
			// infoType: 0 火情弹框 1 设备预计
			// websoclet
			// {"type":"hello","message":"欢迎YH2c918083796a6e5301796a6e54b20002加入连接！"}
			// {"type":"notice","message":"您有一个待处置的消防隐患，请立即前往现场处置","noticeInfoId":"YHff8080817fba2e3a017fbb77d8db0483","infoType":4}
			// {"type":"notice","equRounds":"false","audio":"false","message":"第二灭火员你好，你旁边的商场四层区域发生的火灾已结束!","noticeInfoId":"TZ_XQff8080818022465a018022ba0476051b","audioId":"2","audioOpen":"false","equipmentId":"null","drill":"false","taskId":"null","infoType":"0"}
			// {"type":"notice","equRounds":"true","audio":"true","message":"巡检员你好，你所负责的商场三层区域的设备813056三层8308点型感烟发起火情预警，请尽快前往此处查看确认情况。",
			// "noticeInfoId":"TZ_XQff8080818022465a018022bad5390531","audioId":"4","audioOpen":"false","equipmentId":"EQU402881117a1df8ed017a1e00f36b0435","drill":"false","taskId":"QYRW402870bb7d0d7466017d2e3f96e34606","alarmInfoId":"YJff8080818022465a018022bad52a052f","infoType":"1"}
			let app = getApp()
			let uid = app.globalData.getToken("uid")
			uni.connectSocket({
				url: constants.WEBSOCKETAPI + uid
			});

			let that = this
			uni.onSocketMessage(function(res) {

				let data = JSON.parse(res.data)

				if (res.data.audio == 'true') {
					console.info("音乐响起:" + res.data.audio)
					that.playAudio("https://www.cetczhxf.com/mp3/alarm.mp3")
				}

				let obj = JSON.parse(res.data)
				if (obj.hasOwnProperty('equRounds') && obj.equRounds == 'true') {
					console.info(">>>>>>>>>>>>>>>>," + JSON.stringify(obj))
					console.info(obj.alarmInfoId + "xxxxxxxxxxxx")
					that.equRounds = true
					console.info("xxccc" + that.equRounds)
					that.ucId = obj.alarmInfoId

				}

				if (obj.infoType == "2") {
					console.info("9999999999999999999999999")
					that.alertModal = 'show'
					// 一键报警
					that.alertTitle = '发现火情'
					that.alertMsg = obj.message
					that.alertImg = 'https://ifire-oss.oss-cn-shanghai.aliyuncs.com/image/banner3.png'
					that.noticeInfoId = obj.noticeInfoId
					that.audioId = obj.audioId
				}
				if (obj.infoType == "1") {
					// 设备预警
					console.info("888888888888888888888888888")

					that.alertModal = 'show'
					that.tipContent = '设备预警'
					that.alertMsg = obj.message
					that.alertImg = "https://ifire-oss.oss-cn-shanghai.aliyuncs.com/image/banner1.png"
					that.noticeInfoId = obj.noticeInfoId
					that.audioId = obj.audioId

				}
				if (obj.infoType == "3") {
					// 设备故障
					console.info("77777777777777777777777777")

					that.alertModal = 'show'
					that.tipContent = '设备预警'
					that.alertMsg = obj.message
					that.alertImg = "https://ifire-oss.oss-cn-shanghai.aliyuncs.com/image/banner2.png"
					that.noticeInfoId = obj.noticeInfoId
					that.audioId = obj.audioId
				}
			});
		}
	}
</script>

<style scoped>
	.bg-img {
		height: 400rpx;
	}
</style>
